package Demo1;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class detailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        // 做准备工作:
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print("  <meta charset='UTF-8'>");
out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print("    <title>Document</title>");
out.print("    <style>");
out.print("                table{");
out.print("            border: 2px solid gray;");
out.print("            margin:30px auto;");
out.print("            border-collapse: collapse;");
out.print("            text-align: center;");
out.print("        }");
out.print("        tr,td,th{");
out.print("            border: 2px solid gray;");


out.print(" </style>");
out.print("</head>");
out.print("<body>");
out.print("    <!-- 某一条记录的详细信息: -->");
out.print("    <table width='50%'>");
out.print("        <tr>");
out.print("            <th>部门号</th>");
out.print("            <th>名称</th>");
out.print("            <th>工作地点</th>");
out.print("     </tr>");
out.print("       <tbody>");

        try {
            // 获取前端数据得到的值:
            String dno = req.getParameter("deptno");
            connection = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept where deptno = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,dno);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                String deptno = resultSet.getString("deptno");
                String dname = resultSet.getString("dname");
                String loc = resultSet.getString("loc");


                out.print("           <tr>");
                out.print("               <td>"+deptno+"</td>");
                out.print("              <td>"+dname+"</td>");
                out.print("             <td>"+loc+"</td>");
                out.print("            </tr>");


            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
            }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        out.print("        </tbody>");
        out.print("   </table>");
        out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
        out.print("</body>");
        out.print("</html>");
        }
    }

